package com.jiudaoliu.homeprototype.service.impl;

import com.jiudaoliu.homeprototype.POJO.VO.CommonVO;
import com.jiudaoliu.homeprototype.entity.*;
import com.jiudaoliu.homeprototype.enums.SOURCE_TYPE_ENUM;
import com.jiudaoliu.homeprototype.enums.STATUS_ENUM;
import com.jiudaoliu.homeprototype.repository.*;
import com.jiudaoliu.homeprototype.service.SourceInfoServie;
import com.jiudaoliu.homeprototype.utils.CommonVO_Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * @Package com.jiudaoliu.homeprototype.service.impl
 * @Description:
 * @Author 陈建
 * @Create: 2018-09-20 13:19
 * @Version 1.0
 * @Update: message
 */
@Service
@Transactional
public class SourceInfoServieImpl implements SourceInfoServie {

    @Autowired
    private SourceInfoRepository sourceInfoRepository;

    @Autowired
    private SourceTypeRepository sourceTypeRepository;

    @Autowired
    private ModelRepository modelRepository;

    @Autowired
    private LabelRepository labelRepository;

    @Autowired
    private LabelListRepository labelListRepository;

    @Override
    public CommonVO add(SourceInfoEntity sourceInfo) {
        //校验数据(类型,模块)
        if (null != sourceInfo) {
            if (null != sourceInfo.getModelId()) {
                //模块
                Optional<ModelEntity> optional = modelRepository.findById(sourceInfo.getModelId());
                if (!optional.isPresent()) {
                    return CommonVO_Utils.fail_500("模块不存在");
                }
            } else {
                return CommonVO_Utils.fail_404("模块数据异常");
            }
            if (null != sourceInfo.getSourceTypeId()) {
                //类型
                Optional<SourceTypeEntity> optional = sourceTypeRepository.findById(sourceInfo.getSourceTypeId());
                if (!optional.isPresent()) {
                    return CommonVO_Utils.fail_500("资源类型不存在");
                }
            } else {
                return CommonVO_Utils.fail_404("资源类型异常");
            }

        }
        //设置默认状态
        sourceInfo.setStatus(STATUS_ENUM.STATUS_USE.getStatus());
        sourceInfo.setCreateTime(System.currentTimeMillis());

        //保存数据
        SourceInfoEntity sourceInfoEntity = sourceInfoRepository.saveAndFlush(sourceInfo);

        //更新标签列表
        //初始化数据
        List<LabelEntity> list = labelRepository.findByLabelName(SOURCE_TYPE_ENUM.TYPE_DEF.getType());
        Long labelId = null;
        if (null != list && list.size() > 0) {
            LabelEntity labelEntity = list.get(0);
            labelId = labelEntity.getId();
        } else {
            //新增
            LabelEntity labelEntity = new LabelEntity();
            labelEntity.setCode(SOURCE_TYPE_ENUM.TYPE_DEF.getCode());
            labelEntity.setLabelName(SOURCE_TYPE_ENUM.TYPE_DEF.getType());
            LabelEntity newlabelEntity = labelRepository.saveAndFlush(labelEntity);
            labelId = newlabelEntity.getId();
        }
        LabelList labelList = new LabelList();
        labelList.setLabeTypelId(labelId);
        labelList.setSourceInfoId(sourceInfoEntity.getId());
        labelListRepository.saveAndFlush(labelList);
        return CommonVO_Utils.success(sourceInfoEntity);
    }

    @Override
    public CommonVO findById(Long id) {
        Optional<SourceInfoEntity> optional = sourceInfoRepository.findById(id);
        if (optional.isPresent()) {
            return CommonVO_Utils.success(optional.get());
        }
        return CommonVO_Utils.fail_404();
    }

    @Override
    public CommonVO findAllByModelid(Long id) {
        List<SourceInfoEntity> list = sourceInfoRepository.findByModelId(id);
        if (null != list && list.size() > 0) {
            return CommonVO_Utils.success(list);
        }
        return CommonVO_Utils.fail_404();
    }

    @Override
    public CommonVO findByStatusAndModelid(String status, Long id) {
        List<SourceInfoEntity> list = sourceInfoRepository.findByStatusAndModelId(status, id);
        if (null != list && list.size() > 0) {
            return CommonVO_Utils.success(list);
        }
        return CommonVO_Utils.fail_404();
    }

}
